package zhang.lao.console.controller;/**
 * Created by zhangqin on 2017/8/21.
 */

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import zhang.lao.build.mybatis.jdbc.auto.dao.WnMachineMapper;
import zhang.lao.build.mybatis.jdbc.auto.dao.WnMachineToolMapper;
import zhang.lao.build.mybatis.jdbc.auto.model.*;
import zhang.lao.build.tool.Point;
import zhang.lao.dao.base.WnFarmlandDao;
import zhang.lao.dao.base.WnLnglatAutomaticDao;
import zhang.lao.dao.base.WnLnglatGt06Dao;
import zhang.lao.pojo.console.map.PointResp;
import zhang.lao.pojo.console.resp.HttpResult;
import zhang.lao.pojo.console.resp.HttpResultUtil;
import zhang.lao.service.sequence.SequenceTool;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;

/**
 * 测试controller
 *
 * @author
 * @create 2017-08-21 15:04
 **/
@RestController
public class TestController {
    @Resource
    private WnLnglatAutomaticDao wnLnglatAutomaticDao;

    @Resource
    private WnLnglatGt06Dao wnLnglatGt06Dao;
    @Resource
    private WnFarmlandDao wnFarmlandDao;
    @Resource
    private WnMachineToolMapper wnMachineToolMapper;

    @Resource
    private WnMachineMapper wnMachineMapper;

    @Resource
    private SequenceTool sequenceTool;
    @RequestMapping("/test/init/number")
    public @ResponseBody HttpResult initModelNumber(){

        List<WnMachine> wnMachines = wnMachineMapper.selectByExample(null);
       int i = 1;
        for (WnMachine wnMachine : wnMachines) {
            wnMachine.setMachineNumber(sequenceTool.getSequence("MACHINE_NUMBER")+"");
            wnMachineMapper.updateByPrimaryKey(wnMachine);

        }
        List<WnMachineTool> wnMachineTools = wnMachineToolMapper.selectByExample(null);
        for (WnMachineTool wnMachineTool : wnMachineTools) {
            wnMachineTool.setToolNumber(sequenceTool.getSequence("MACHINE_TOOL_NUMBER")+"");
            wnMachineToolMapper.updateByPrimaryKey(wnMachineTool);
        }
        return HttpResultUtil.buildSuccess();
    }
  /*  @RequestMapping("/init/nt")
    public @ResponseBody HttpResult initNt(){
        List<WnFarmland> wnFarmlands = wnFarmlandDao.selectByExample(null);

        for (WnFarmland wnFarmland : wnFarmlands) {
            String centerLatlng = wnFarmland.getCenterLatlng();
            String[] split1 = centerLatlng.split(",");
            JSONObject j2 = new JSONObject();
            j2.put("lat",Double.parseDouble(split1[0].trim()+""));
            j2.put("lng",Double.parseDouble(split1[1].trim()+""));
            wnFarmland.setCenterLatlng(JSON.toJSONString(j2));
            List<JSONObject> list = Lists.newArrayList();
        if(wnFarmland.getLatlngs().indexOf("$")!=-1){
            String latlngs = wnFarmland.getLatlngs();
            String[] ss = latlngs.split("\\$");
            if(ss.length>0){
                for (String s : ss) {
                    if(s.indexOf("!")!=-1){
                        String[] split = s.split("\\!");
                        JSONObject j = new JSONObject();
                        j.put("lng",Double.parseDouble(split[1].trim()));
                        j.put("lat",Double.parseDouble(split[0].trim()));
                        list.add(j);
                    }
                }
            }
        }
            wnFarmland.setLatlngs(JSON.toJSONString(list));
            wnFarmlandDao.updateByPrimaryKey(wnFarmland);
        }
        return HttpResultUtil.buildSuccess();
    }
*/
/*    @RequestMapping("/img_upload")
    public JSONObject img_upload(String data)
    {
        System.out.println("图片-------"+data);
        return  getSuccessResult("http://wwww.baidu.com"+new Date().getTime());
    }
    @RequestMapping("/body_upload")
    public JSONObject body_upload(String data){
        System.out.println("身材-------"+data);
        return  getSuccessResult();
    }
    private JSONObject getSuccessResult(){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("flag",1);
//        jsonObject.put("errorCode","");
//        jsonObject.put("errorMsg","");
        return jsonObject;
    }
    private JSONObject getSuccessResult(String pic){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("flag",1);
//        jsonObject.put("errorCode","");
//        jsonObject.put("errorMsg","");
        JSONObject jsonObjectBody = new JSONObject();
        jsonObjectBody.put("picUrl",pic);
        jsonObject.put("data",jsonObjectBody);
        return jsonObject;
    }

    private JSONObject getErrorResult(String code , String message){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("flag",0);
        jsonObject.put("errorCode",code);
        jsonObject.put("errorMsg",message);
        return jsonObject;
    }*/

/*    @RequestMapping("/init/latlng/{id}")
    public @ResponseBody HttpResult initLatLng(@PathVariable int id,double slng,double slat){
        List<Point> pointList = Lists.newArrayList();
        List<Point> points = runRow(slng, slat, 20, 20, pointList);
        long dateTime = new Date().getTime();
        for (int i = 0; i < points.size(); i++) {
            Point point = points.get(i);
            WnLnglatAutomatic wnLnglatGt06 = new WnLnglatAutomatic();
            if(i==0){
                wnLnglatGt06.setOrentation(90);
            }else{
                Point point2 = points.get(i-1);
                wnLnglatGt06.setOrentation((new Double(getAngle1(point.getLat(),point.getLng(),point2.getLat(),point2.getLng())).intValue()));
            }
            dateTime+=10000;
            wnLnglatGt06.setDeviceId(id);
            wnLnglatGt06.setCreateTime(new Date(dateTime));
            wnLnglatGt06.setWorkWidth(1.00);
            wnLnglatGt06.setAbLineId(1);
            wnLnglatGt06.setControlType(1+"");
            wnLnglatGt06.setHeight(0.00);
            wnLnglatGt06.setLat(point.getLat()+"");
            wnLnglatGt06.setLng(point.getLng()+"");
            wnLnglatGt06.setSpeed(30.00);
            wnLnglatAutomaticDao.insertSelective(wnLnglatGt06);
        }
        return HttpResultUtil.buildSuccess();
    }
    @RequestMapping("/init/latlng/gt06/{id}")
    public @ResponseBody HttpResult initLatLnggt06(@PathVariable int id,double slng,double slat){
        List<Point> pointList = Lists.newArrayList();
        List<Point> points = runRow(slng, slat, 20, 20, pointList);
        long dateTime = new Date().getTime();
        for (int i = 0; i < points.size(); i++) {
            Point point = points.get(i);
            WnLnglatGt06 wnLnglatGt06 = new WnLnglatGt06();
            if(i==0){
                wnLnglatGt06.setOrentation(90);
            }else{
                Point point2 = points.get(i-1);
                wnLnglatGt06.setOrentation((new Double(getAngle1(point.getLat(),point.getLng(),point2.getLat(),point2.getLng())).intValue()));
            }
            dateTime+=10000;
            wnLnglatGt06.setDeviceId(id);
            wnLnglatGt06.setCreateTime(new Date(dateTime));
            wnLnglatGt06.setDepth( 0.00);
            wnLnglatGt06.setHeight(0.00);
            wnLnglatGt06.setLat(point.getLat()+"");
            wnLnglatGt06.setLng(point.getLng()+"");
            wnLnglatGt06.setSpeed(30.00);
            wnLnglatGt06Dao.insertSelective(wnLnglatGt06);
        }
        return HttpResultUtil.buildSuccess();
    }


    private static List<Point> runRow(double startLng, double startLat,int row ,int column, List<Point> pointList){
        double add = 0.0001;
        for (int i = 0; i < row; i++) {
            if(row%2!=0){
                for (int j = column; j >0 ; j--) {
                    Point point = new Point(startLng+(add*j),startLat+(add*i));
                    pointList.add(point);
                }
            }else{
                for (int j = 0; j <column ; j++) {
                    Point point = new Point(startLng+(add*j),startLat+(add*i));
                    pointList.add(point);
                }
            }


        }
        return pointList;
    }


    private static double getAngle1(double lat_a, double lng_a, double lat_b, double lng_b) {

        double y = Math.sin(lng_b-lng_a) * Math.cos(lat_b);
        double x = Math.cos(lat_a)*Math.sin(lat_b) - Math.sin(lat_a)*Math.cos(lat_b)*Math.cos(lng_b-lng_a);
        double brng = Math.atan2(y, x);

        brng = Math.toDegrees(brng);
        if(brng < 0)
            brng = brng +360;
        return brng;

    }*/


}
